 //How to navigate from popup window to other popup window and then come back to main window?
//http://www.hdfc.com/

import java.io.File;
import java.sql.Driver;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.junit.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;


public class WindowHandler{
	  //WebDriver driver;
	  String baseUrl;
	   
	@Before
	public void setUp() throws Exception {
		}

	@Test
	public void windowhandler() throws Exception {
		File profileDir = new File("C:/Users/home/AppData/Roaming/Mozilla/Firefox/Profiles/upi78mth.default");
		FirefoxProfile profile = new FirefoxProfile(profileDir);
		WebDriver driver = new FirefoxDriver(profile);
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		baseUrl = "http://www.hdfc.com/";
		driver.navigate().to(baseUrl);
		driver.findElement(By.xpath("//*[@id='maincontent']/div[1]/ul/li[1]/a/img")).click();
		Thread.sleep(2000);
		Set<String> windowids = driver.getWindowHandles();
		Iterator<String> iter = windowids.iterator();
		String mainWindowId = iter.next();
		String tabedWindowId = iter.next();
		Thread.sleep(2000L);
		driver.switchTo().window(tabedWindowId);
		//driver.findElement(By.xpath("html/body/table/tbody/tr[2]/td/table/tbody/tr/td[3]/table/tbody/tr/td/table/tbody/tr[1]/td/table/tbody/tr[2]/td/p[2]/a")).click();
		Thread.sleep(2000);
		driver.switchTo().window(mainWindowId);
		Thread.sleep(2000);
		driver.switchTo().window(tabedWindowId);
		driver.findElement(By.xpath("//*[@id='txtName']")).sendKeys("Maruthi Ram");
		Thread.sleep(4000);
	}

    @After
    public void tearDown() throws Exception {
    	//driver.close();
    	//driver.quit();
      }
	}